import Vue from "vue";

// 谷歌跟踪埋点
Vue.prototype.$gmeRepore = (action, query) => {
    const option = {
        register: { send_to: "AW-10829089541/QATRCJC54acDEIWW26so" },
        whatsapp: { send_to: "AW-10829089541/X1KkCITvguADEIWW26so" },
        addToStore: { send_to: "AW-10829089541/EX_uCO22zsgDEIWW26so" },
        orderPaid: {
            send_to: "AW-10829089541/30b8CL3OvaIDEIWW26so",
            value: 1.0,
            currency: "USD",
            transaction_id: "",
        },
    };

    if (!action || !option[action]) {
        // 没有对应的
        return Promise.resolve();
    } else if (!window.gtag) {
        // 谷歌跟踪没加载好
        return Promise.resolve();
    } else {
        return new Promise((resolve) => {
            const request = {
                ...option[action],
                ...(query || {}),
                event_callback: () => resolve(),
            };
            window.gtag("event", "conversion", request);
        });
    }
};
